package actions;

import java.util.ArrayList;
import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.BookManager;
import beans.BookManagerBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;

public class SaveBookAction extends ActionSupport {

	@EJB
	private BookManager bookManager;

	private Item item = new Item();
	private boolean save = false;
	
	public void setItem(Item item) {
		this.item = item;
	}
	
	public Item getItem() {
		return this.item;
	}
	
	public String execute() {

		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");
		if ( id == null ) {
			this.addActionError("User ID is null");
			return ERROR;
		}

		bookManager = new BookManagerBean();
		if ( save ) {
			if ( bookManager.updateCopy(item) ) {
				return SUCCESS;
			}
			this.addActionError("Book could not be saved");
			return "ErrorInChange";			
		}
		return SUCCESS;
	}

	public boolean isSave() {
		return save;
	}

	public void setSave(boolean save) {
		this.save = true;
	}

}
